﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 委托
{
    class Program
    {
        // 委托其实是一种类型，用来存储函数引用
        // 有点类似于函数指针
        delegate double ProcessDelegate(double param1, double param2);
        static double Multiply(double param1, double param2) => param1 * param2;
        static double Divide(double param1, double param2) => param1 / param2;


        static void Main(string[] args)
        {
            Console.WriteLine("Enter 2 numbers separated with a comma:");
            string input = Console.ReadLine();
            int commapos = input.IndexOf(',');

            double param1 = Convert.ToDouble(input.Substring(0,commapos));
            double param2 = Convert.ToDouble(input.Substring(commapos+1, input.Length - commapos - 1));
            Console.WriteLine("Enter M to multiply or D to divide:");

            // 定义一个委托类型的变量
            ProcessDelegate process;
            input = Console.ReadLine();

            if(input == "M")
            {
                // 给委托变量赋值
                process = new ProcessDelegate(Multiply);
                Console.WriteLine("param1 * param2");
            }
            else
            {
                process = new ProcessDelegate(Divide);
                Console.WriteLine("param1 / param2");
            }

            double d = process(param1, param2);

            Console.WriteLine($"Result:{d}");

            Console.ReadKey();
        }
    }
}
